/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data_access;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author faraz
 */
public class RDBCustomerDAO implements CustomerDAO{

    private Connection dbConnection = null;

    public RDBCustomerDAO(Connection connection) {
        this.dbConnection = connection;
    }
    
    @Override
    public void createCustomer(Customer customer) {
        try {
            PreparedStatement sqlStatement = dbConnection.prepareStatement(
                    "INSERT INTO CUSTOMER(CUSTOMER_ID, CUSTOMER_NAME)"
                    + " VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
            sqlStatement.setInt(1, customer.customerId);
            sqlStatement.setString(2, customer.customerName);
            sqlStatement.executeUpdate();
            ResultSet result = sqlStatement.getGeneratedKeys();
            result.next();
            customer.customerId = result.getInt(1);
        } catch (SQLException sqlException) {
            System.out.println("Could not add new customer.");
            sqlException.printStackTrace();
        }
    }    
}
